<template>
  <a-upload
    list-type="picture-card"
    :show-upload-list="true"
    :action="actionUrl"
    :before-upload="beforeUpload"
    @preview="handlePreview"
    @change="handleChange"
    :remove="handRemove"
    :default-file-list="defaultFileList"
  >
    <!-- <img v-if="imageUrl" :src="imageUrl" alt="avatar" class="avatarImg" /> -->
    <div>
      <a-icon :type="loading ? 'loading' : 'plus'" />
      <div class="ant-upload-text">Upload</div>
    </div>
  </a-upload>
</template>
<script>
import axios from 'axios'

export default {
  data() {
    return {
      actionUrl: this.$host + '/api/UploadFile',
      loading: false,
      imageUrl: '',
      defaultFileList: [],
    }
  },
  props: {
    imageArr: {
      type: Array,
      default: () => [],
    },
    propImageUrl: {
      type: String,
      default: '',
    },
  },
  watch: {
    propImageUrl(val) {
      this.imageUrl = this.propImageUrl
    },
  },
  methods: {
    handlePreview(info) {
      console.log('info :>> ', info)
    },
    // 删除文件
    deleteFile(fileName) {
      const a = {
        method: 'DelFile',
        userId: window.userId || 1,
        sign: '1',
        currentPage: 0,
        limit: 0,
        datas: {
          fullFileName: fileName,
        },
      }
      const url = this.$host + '/api/DelFile'
      axios.post(url, JSON.stringify(a)).then((res) => {
        if (res.data.code === 0) {
          console.log('删除成功')
          this.$message.success('Delete Success!')
        } else {
          console.log('删除失败')
          this.$message.error(res.data.description)
        }
        this.$emit('getFileHttpUrl', '')
      })
    },
    handRemove(file) {
      console.log('删除', file)
      if (file.response) {
        this.deleteFile(file.response.data.fileName)
      } else if (file.url) {
        this.deleteFile(file.url)
      }
    },
    handleChange(info) {
      if (info.file.status === 'uploading') {
        this.loading = true
        return
      }
      if (info.file.status === 'done') {
        this.$emit('fileSuccess', info.file.response.data)
        this.loading = false
        console.log('info :>> ', info)

        // Get this url from response in real world.
      }
    },
    beforeUpload(file) {
      let that = this
      let isPdf = file.type === 'application/pdf'
      console.log('isPdf :>> ', isPdf)
      console.log('file.size  :>> ', file.size / 1024 / 1024)
      return new Promise((resolve, reject) => {
        if (!isPdf) {
          that.$message.warning(
            'The file format is incorrect. Please upload the PDF file'
          )
          that.defaultFileList = that.defaultFileList.splice(
            that.defaultFileList.length - 1,
            1
          )
          console.log('this.fileList :>> ', that.defaultFileList)
          reject(false)
        }
        resolve(true)
      })
    },
  },
}
</script>
<style lang="less" scoped>
.avatarImg {
  width: 102px;
  height: 102px;
}
.ant-upload-picture-card-wrapper {
  width: auto !important;
}
</style>
